﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;

namespace Halcon二次开发.Model.自定义控件
{
    /// <summary>
    /// 实时监控数据
    /// </summary>
    public partial class AllDatasShow : UserControl
    {
        //总数
        public double sumnumber;
        //合格总数
        private double okNumber;
        //不合格总数
        private double ngNumber;
        //当前工位名称
        private string name;
        //OK或NG数据
        private List<bool> OKNGPaer;
        private AllDatasShow()
        {
            InitializeComponent();
        }
        public AllDatasShow(string name,List<bool> OKNGPaer)
        {
            InitializeComponent();
            this.OKNGPaer = OKNGPaer;
            this.name = name;
            AllDataLoad();
        }
        //
        private void AllDataLoad()
        {
            if (OKNGPaer == null)
            {
                if (OKNGPaer.Count == 0)
                {
                    return;
                }
                return;
            }
            for (int i = 0; i < OKNGPaer.Count; i++)
            {
                if (OKNGPaer[i])
                {
                    okNumber += 1;
                }
                else
                {
                    ngNumber += 1;
                }
            }
            // 设置Chart控件的基本属性  
            chart1.ChartAreas.Clear();
            ChartArea chartArea = new ChartArea();
            chart1.ChartAreas.Add(chartArea);
            chart1.Titles[0].Text = name+"工位实时数据监控";
            //计算合格率
            chart1.Titles[1].Text = "总数:" + OKNGPaer.Count;
            double text = okNumber/OKNGPaer.Count*100;

            chart1.Titles[2].Text = "良品率:" + text.ToString("F2") + "%";
            chart1.Titles[3].Text = "不良品率:" + (100 - text).ToString("F2") + "%";
            // 创建数据系列 2
            Series series = new Series();
            series.ChartType = SeriesChartType.Pie;
            series.Name = "Status";

            // 添加数据点到系列中  
            // 注意：第一个参数是数据点的值，第二个参数是数据点的标签  
            series.Points.AddXY("合格:" + okNumber, okNumber);
            series.Points.AddXY("不合格:" + ngNumber, ngNumber);

            // 将系列添加到Chart控件中  
            chart1.Series.Add(series);
            sumnumber = okNumber + ngNumber;
        }
        /// <summary>
        /// 将数据加载到统计图上
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void AllDatasShow_Load(object sender, EventArgs e)
        {
            
        }
    }
}
